package com.xuesinuo.ots.api;

import java.util.Arrays;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xuesinuo.ots.config.auth.Auth;
import com.xuesinuo.ots.config.auth.AuthI18n;
import com.xuesinuo.ots.config.pojo.IdEntity;
import com.xuesinuo.ots.config.spring.ApiResultPacking;
import com.xuesinuo.ots.entity.Role;
import com.xuesinuo.ots.pojo.RolePojo;
import com.xuesinuo.ots.tool.UserTool;
import com.xuesinuo.pignoo.core.Pignoo;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import lombok.RequiredArgsConstructor;

/** 后台角色与权限 */
@RestController
@RequestMapping("/backend/role")
@ApiResultPacking
@RequiredArgsConstructor
public class RoleApi {
    private final UserTool authTool4User;
    private final Pignoo pignoo;

    @Data
    public static class GetAllAuthResult {
        private List<AuthI18n> authList;
    }

    /** 查看全部权限字符 */
    @Auth
    @GetMapping("/getAllAuth")
    public GetAllAuthResult getAllAuth() {
        List<AuthI18n> authList = authTool4User.getAllI18nAuth();
        GetAllAuthResult result = new GetAllAuthResult();
        result.setAuthList(authList);
        return result;
    }

    @Data
    public static class GetAllRoleResult {
        private List<RolePojo> roleList;
    }

    /** 查询全部角色 */
    @Auth
    @GetMapping("/getAllRole")
    public GetAllRoleResult getAllRole() {
        List<Role> roleList = pignoo.reader(Role.class).getAll();
        GetAllRoleResult result = new GetAllRoleResult();
        result.setRoleList(roleList.stream().map(r -> RolePojo.init(r)).toList());
        return result;
    }

    @Data
    public static class AddParam {
        @NotBlank
        private String name;
        private List<String> menuList;
        private List<String> authList;
    }

    /** 新建角色信息 */
    @Auth("Auth:Edit")
    @PostMapping("/add")
    public RolePojo addRole(@RequestBody @Validated AddParam param) {
        if (param.getMenuList() == null || param.getMenuList().isEmpty()) {
            param.setMenuList(Arrays.asList());
        }
        if (param.getAuthList() == null || param.getAuthList().isEmpty()) {
            param.setAuthList(Arrays.asList());
        }
        RolePojo pojo = new RolePojo();
        pojo.setName(param.getName());
        pojo.setMenuList(param.getMenuList());
        pojo.setAuthList(param.getAuthList());
        Role role = pojo.toRole();
        Role newRole = pignoo.writer(Role.class).add(role);
        return RolePojo.init(newRole);
    }

    @Data
    public static class SetParam {
        @NotNull
        private Long id;
        private String name;
        private List<String> menuList;
        private List<String> authList;
    }

    /** 修改角色信息 */
    @Auth("Auth:Edit")
    @PostMapping("/setById")
    public void setById(@RequestBody @Validated SetParam param) {
        Role role = pignoo.writer(Role.class).filter(Role::getId, "==", param.getId()).getAny();
        if (role.getName() != null) {
            role.setName(param.getName());
        }
        RolePojo pojo = new RolePojo();
        pojo.setAuthList(param.getAuthList());
        pojo.setMenuList(param.getMenuList());
        if (param.getMenuList() != null) {
            role.setMenuJson(pojo.getMenuJson());
        }
        if (param.getAuthList() != null) {
            role.setAuthJson(pojo.getAuthJson());
        }
    }

    /** 删除角色 */
    @Auth("Auth:Edit")
    @PostMapping("/delById")
    public void delById(@RequestBody @Validated IdEntity param) {
        pignoo.writer(Role.class).filter(Role::getId, "==", param.getId()).removeAll();
    }
}
